package com.share.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.share.common.model.sysinfo.Server;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

@Slf4j
public class SysUtil {
    private static Integer SCREEN_HEIGHT;//屏幕高度
    private static Integer SCREEN_WITH; //屏幕宽度
    private static int CPU_CORE; //cpu核心数
    private static String MACHINE_CODE; //机器码
    private static boolean IS_WINDOWS; //是否为windows

    static {
        //windows判断
        boolean isWindows = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindows = true;
        }
        IS_WINDOWS = isWindows;

        //cpu核心数
        CPU_CORE = Runtime.getRuntime().availableProcessors();

        //机器码计算
        Process process = null;
        String machineCode = null;
        if (isWindows) {
            //cpu的序列号
            String cpuSn = null;
            try {
                process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
                process.getOutputStream().close();
                Scanner sc = new Scanner(process.getInputStream());
                String serial = sc.next();
                cpuSn = sc.next();
            } catch (Exception e) {
                throw new RuntimeException("获取CPU序列号失败");
            }

            //硬盘的序列号
            String hardSn = null;
            try {
                process = Runtime.getRuntime().exec(new String[]{"wmic", "path", "win32_physicalmedia", "get", "serialnumber"});
                process.getOutputStream().close();
                Scanner sc = new Scanner(process.getInputStream());
                String serial = sc.next();
                hardSn = sc.next();
            } catch (Exception e) {
                throw new RuntimeException("获取硬盘序列号失败");
            }
            machineCode = Md5Util.encodeByMD5(cpuSn + "_" + hardSn);
        } else {
            //bios版本信息
            String boisVersion = null;
            try {
                // 管道
                String result = "";
                process = Runtime.getRuntime().exec("sudo dmidecode -s bios-version");
                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    result += line;
                    break;
                }
                br.close();
                boisVersion = result;
            } catch (Exception e) {
                System.out.println("获取主板信息错误");
            }

            //系统信息
            String osSn = null;
            try {
                String result = null;
                process = Runtime.getRuntime().exec("sudo dmidecode -s system-uuid");
                InputStream in = process.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                while (in.read() != -1) {
                    result = br.readLine();
                }
                br.close();
                in.close();
                process.destroy();
                osSn = result;
            } catch (Throwable e) {
                e.printStackTrace();
            }
            machineCode = Md5Util.encodeByMD5(boisVersion + "_" + osSn);
        }
        MACHINE_CODE = machineCode;
    }


    //这个需要懒加载
    private static void initScreen() {
        // 获取电脑屏幕宽高
        try {
            //Headless模式是系统的一种配置模式。在该模式下，系统缺少了显示设备、键盘或鼠标
            //java.lang.UnsatisfiedLinkError: /usr/local/jdk1.8.0_60/jre/lib/amd64/libawt_xawt.so: libXrender.so.1
            System.setProperty("java.awt.headless", "true");//默认为true
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            SCREEN_WITH = screenSize.width;
            SCREEN_HEIGHT = screenSize.height;
        } catch (Exception e) {
            log.error("获取屏幕的高和宽失败", e);
        }
        //linux 获取失败给个默认值
        if (!isWindows()) {
            SCREEN_WITH = 1080;
            SCREEN_HEIGHT = 1920;
            log.error("linux 环境给定默认屏幕的高={},宽={}", SCREEN_HEIGHT, SCREEN_WITH);
        }
    }

    public static int getScreenHeight() {
        if (SCREEN_HEIGHT == null) {
            initScreen();
        }
        return SCREEN_HEIGHT;
    }

    public static int getScreenWith() {
        if (SCREEN_WITH == null) {
            initScreen();
        }
        return SCREEN_WITH;
    }

    public static int getCpuCore() {
        return CPU_CORE;
    }

    public static String getMachineCode() {
        return MACHINE_CODE;
    }

    //是否为windows环境
    public static boolean isWindows() {
        return IS_WINDOWS;
    }

    public static JSONObject server() {
        Server server = new Server();
        try {
            server.copyTo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(JSON.toJSONString(server));
    }

    public static void main(String[] args) {
        System.out.println(server());
    }
}
